package com.lxlg.lxlg_order.consumer;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.lxlg.lxlg_order.alipay.config.AlipayConfig;
import com.lxlg.lxlg_order.client.param.OrderPayBackParam;
import com.lxlg.lxlg_order.common.constvalue.AllOrderConstValue;
import com.lxlg.lxlg_order.service.service.GoodsOrderService;
import com.lxlg.lxlg_order.service.service.RechargeVipService;
import com.lxlg.lxlg_order.service.service.SeckillOrderService;
import lombok.SneakyThrows;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//支付回调
@Component
@RocketMQMessageListener(consumerGroup = "Check_Order_pay",topic = "Check_Order_pay")
public class CheckAllOrderPay implements RocketMQListener<OrderPayBackParam>, RocketMQPushConsumerLifecycleListener {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private RechargeVipService rechargeVipService;
    @Resource
    private GoodsOrderService goodsOrderService;
    @Resource
    private SeckillOrderService seckillOrderService;

    @SneakyThrows
    @Override
    public void onMessage(OrderPayBackParam orderPayBackParam) {

        //查看redis中订单状态"recharge_pay:"+out_trade_no
        //"order_pay:"+out_trade_no+"order_type"+AllOrderConstValue.order_type_form,"0"
        String state = (String)redisTemplate.opsForValue().get("order_pay:"+orderPayBackParam.getOrderCode()+"order_type"+orderPayBackParam.getOrderType());
        if(state==null||!state.equals("0")){//已经支付完毕
            return;
        }
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //查看支付宝是否已经处于支付宝状态
        /**
         */
            //获得初始化的AlipayClient
            AlipayTradeQueryRequest alipayQueryRequest = new AlipayTradeQueryRequest();
            String biz_content = "{\"out_trade_no\":\"" + orderPayBackParam.getOrderCode() + "\"}";
            alipayQueryRequest.setBizContent(biz_content);
            AlipayTradeQueryResponse alipayQueryResponse = null;
            try {
                alipayQueryResponse = alipayClient.execute(alipayQueryRequest);

                if (null != alipayQueryResponse && alipayQueryResponse.isSuccess()) {
                    if (alipayQueryResponse.getCode().equals("10000")) {
                        if ("TRADE_SUCCESS".equalsIgnoreCase(alipayQueryResponse
                                .getTradeStatus())) {
                            System.out.println("进入回调准备修改状态");
                            if (orderPayBackParam.getOrderType()== AllOrderConstValue.order_type_recharge) {
                                rechargeVipService.rechargerPayBack(orderPayBackParam.getOrderCode());
                            }else if(orderPayBackParam.getOrderType()== AllOrderConstValue.order_type_form){
                                goodsOrderService.formorderPayBack(orderPayBackParam.getOrderCode());
                            }else {
                                seckillOrderService.seckillorderPayBack(orderPayBackParam.getOrderCode());
                            }
                            //表示支付成功
                        } else if ("TRADE_CLOSED".equalsIgnoreCase(alipayQueryResponse
                                .getTradeStatus())) {
                            // 表示未付款关闭，或已付款的订单全额退款后关闭
                        } else if ("TRADE_FINISHED".equalsIgnoreCase(alipayQueryResponse
                                .getTradeStatus())) {
                            // 此状态，订单不可退款或撤销
                        }
                    } else {
                        // 如果请求未成功，请重试

                    }
                }
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }

    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        //设置重试次数
        consumer.setMaxReconsumeTimes(1);
    }
}
